package com.jie.eclipse.pan.editors.assist.contributor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;

import com.jie.eclipse.pan.editors.assist.data.Item;
import com.jie.eclipse.pan.editors.utils.PanImageHelper;


/**
 * IAssistantContributor对应的default adapter类。
 * 说明：本类只是实现了较为简单的提示情况
 *
 * @author zhuxing (mailto:zhu_xing@live.cn)
 */
/*
 * 修改历史
 * $Log$ 
 */
public abstract class AbstractAssistantContributor implements
		IAssistantContributor {
	
	/* 
	 * 子类可以覆写，提供较为简单的模版方法，分为计算替代字符串和构建completion proposals两步。
	 * 
	 * @see jspeditor.assist.contributor.IAssistantContributor#computeProposals(java.lang.String, java.lang.String, org.eclipse.jface.text.ITextViewer, int)
	 */
	public ICompletionProposal[] computeProposals(IDOMElement element, IDOMAttr attrNode,String tagName,String attrbuteName, String inputText, ITextViewer viewer, int offset) {
		Item[] items = this.computeReplaceStrings(element,attrNode,tagName,attrbuteName, inputText, viewer, offset);
		return this.buildCompletionProposals(items,element,attrNode, tagName,attrbuteName,inputText, viewer, offset);
	} 
	
	/**
	 * 子类可以覆写.
	 * 说明：如果需要提供自定义的display string、image等信息，请直接覆写computeProposals方法。
	 * 
	 * @param attrbuteName
	 * @param inputText
	 * @return
	 */
	protected Item[] computeReplaceStrings(IDOMElement element, IDOMAttr attrNode,String tagName,String attrbuteName, String inputText, ITextViewer viewer, int offset) {
		if(attrNode == null || (attrNode !=null && attrbuteName != null&& attrbuteName.equals(inputText))){
			return computeAttributes(element,  attrNode, tagName, attrbuteName,  inputText,  viewer,  offset);
		}else{
			return computeAttributeValues(element,  attrNode, tagName, attrbuteName,  inputText,  viewer,  offset);
		}
	}
	
	protected Item[] computeAttributes(IDOMElement element, IDOMAttr attrNode,String tagName,String attrbuteName, String inputText, ITextViewer viewer, int offset){
		return new Item[0];
	}
	
	protected Item[] computeAttributeValues(IDOMElement element, IDOMAttr attrNode,String tagName,String attrbuteName, String inputText, ITextViewer viewer, int offset){
		return new Item[0];
	}

	/**
	 * 构造ICompletionProposal实例，只提供简单的ICompletionProposal实例
	 * 
	 * @param replaceStrings
	 * @param inputText
	 * @param viewer
	 * @param offset
	 * @return
	 */
	protected ICompletionProposal[] buildCompletionProposals(Item[] items,IDOMElement element, IDOMAttr attrNode,String tagName,String attrbuteName,String inputText, ITextViewer viewer, int offset) {
		if (items == null || items.length == 0)
			return new ICompletionProposal[0];
		//计算ICompletionProposal相关参数
		int replaceOffset = 0;
		int replaceLength = 0;
		if(attrNode != null){
			replaceOffset = attrNode.getValueRegionStartOffset() + 1;
			replaceLength = attrNode.getValueSource().length();
			
			if(replaceOffset<=1){
				replaceOffset = attrNode.getNameRegionStartOffset();
				replaceLength = attrbuteName.length();
			}
		}else{
			replaceOffset = offset;
		}
		
		List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
		for (int i = 0; i < items.length; i++) {
			int cursorPosition = items[i].getText().length()+2;
			ICompletionProposal proposal = new CustomCompletionProposal(items[i].getValue(), replaceOffset, replaceLength, cursorPosition,
					PanImageHelper.getInstance().getImage("icons/gef/eclipse/attribute_obj.gif"), items[i].getText(), null, null,1200);
			proposals.add(proposal);
		}
		return proposals.toArray(new ICompletionProposal[proposals.size()]);
	}
}
